﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLTN.Data;
using DLTN.Entities;

public partial class Admin_Tour_CNTour : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpTour + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
        if (!Page.IsPostBack)
        {

            BindCatalog.BindSubTour(cboSubTour);
            BindCatalog.BindLoaiTour(cboLoaiTour);
            BindCatalog.BindNoiDen(cboDiemDen);
            BindCatalog.BindNoiDi(cboDiemDi);
            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }

            TList<SubTour> TList_Catalogs = DataRepository.SubTourProvider.Find("IdLoaiTour=" + cboLoaiTour.SelectedValue);
            cboSubTour.DataSource = TList_Catalogs;
            cboSubTour.DataTextField = "TenSubTour";
            cboSubTour.DataValueField = "Id";
            cboSubTour.DataBind();
        }
    }
    protected void BindData(int IdCategories)
    {
        using (Tour entTour = DataRepository.TourProvider.GetById(IdCategories))
        {
            using (SubTour ent_SubTour = DataRepository.SubTourProvider.GetById(entTour.IdSubTour))
            {
                using (LoaiTour ent_LoaiTour = DataRepository.LoaiTourProvider.GetById(ent_SubTour.IdLoaiTour))
                {
                    H_CatalogId.Value = entTour.Id.ToString();
                    cboLoaiTour.SelectedValue = ent_LoaiTour.Id.ToString();
                }
            }
            //entTour.LuotXem = Convert.ToInt32(H_LuotXem.Value);
            cboSubTour.SelectedValue = entTour.IdSubTour.ToString();
            txtTieuDeTin.Text = entTour.TieuDe;
            H_AnhDaiDien.Value = entTour.AnhDaiDien;
            imgViewer.ImageUrl = entTour.AnhDaiDien.Length > 0 ? Common.sPathUpTour + entTour.AnhDaiDien : "no_image.jpg";
            txtTomTat.Text = entTour.TomTat;
            txtContent.Text = Server.HtmlDecode(entTour.NoiDung);
            txtThongTin.Text = Server.HtmlDecode(entTour.ThongTin);
            txtDieuKhoan.Text = Server.HtmlDecode(entTour.DieuKhoan);
            cboDiemDi.SelectedValue = entTour.IdDiemDi.ToString();
            cboDiemDen.SelectedValue = entTour.IdDiemDen.ToString();
            txtNgayDi.Text = entTour.NgayDi.ToString("dd/MM/yyyy");
            txtNgayVe.Text = entTour.NgayVe.ToString("dd/MM/yyyy");
            txtGiaTour.Text = entTour.GiaTour.ToString();
            string GioDi = entTour.GioDi;
            string[] arrayStr = GioDi.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
            //=>> chỗ này nó sẽ split ra 1 mảng 3 phần tử
            txtGioDi.Text = arrayStr[0];
            txtPhutDi.Text = arrayStr[1];
            string GioDen = entTour.GioDen;
            string[] arrayStr2 = GioDen.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
            txtGioDen.Text = arrayStr2[0];
            txtPhutDen.Text = arrayStr2[1];
            txtSoCho.Text = entTour.SoCho.ToString();
            chkTrangThai.Checked = entTour.TrangThai;
        }

    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            using (Tour entTour = new Tour() {Id = Convert.ToInt32(H_CatalogId.Value), IdSubTour = Convert.ToInt32(cboSubTour.SelectedValue), DieuKhoan = Server.HtmlDecode(txtDieuKhoan.Text), TieuDe = Common.StandardString(txtTieuDeTin.Text), TomTat = Common.StandardString(txtTomTat.Text), NoiDung = Server.HtmlEncode(txtContent.Text), ThongTin = Server.HtmlDecode(txtThongTin.Text), IdDiemDi = Convert.ToInt32(cboDiemDi.SelectedValue), IdDiemDen = Convert.ToInt32(cboDiemDen.SelectedValue), NgayDi = DateTime.ParseExact(Common.StandardString(txtNgayDi.Text), "dd/MM/yyyy", null), NgayVe = DateTime.ParseExact(Common.StandardString(txtNgayVe.Text), "dd/MM/yyyy", null), GiaTour = Convert.ToDecimal(txtGiaTour.Text), GioDi = String.Format("{0}:{1}", Common.StandardString(txtGioDi.Text), Common.StandardString(txtPhutDi.Text)), GioDen = String.Format("{0}:{1}", Common.StandardString(txtGioDen.Text), Common.StandardString(txtPhutDen.Text)), SoCho = Convert.ToInt32(txtSoCho.Text) })
            {
                DateTime ngaydi = DateTime.ParseExact(Common.StandardString(txtNgayDi.Text), "dd/MM/yyyy", null);
                entTour.NgayDi = DateTime.ParseExact(Common.StandardString(txtNgayDi.Text), "dd/MM/yyyy", null);
                if (txtNgayVe.Text != "")
                {
                    DateTime ngayden = DateTime.ParseExact(Common.StandardString(txtNgayVe.Text), "dd/MM/yyyy", null);
                    if (ngaydi > ngayden)
                    {
                        ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "Thất bại", "alert('Ngày đi không thể lớn hơn ngày đến!');", true);
                        return;
                    }
                    else
                    {
                        entTour.NgayVe = DateTime.ParseExact(Common.StandardString(txtNgayVe.Text), "dd/MM/yyyy", null);
                    }
                }
                if (H_AnhDaiDien.Value.Length == 0)
                {
                    entTour.AnhDaiDien = "no_image.jpg";
                }
                else
                {
                    entTour.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
                }
                entTour.TrangThai = chkTrangThai.Checked;
                entTour.NgayCapNhat = Common.GetServerDateTime();
                if (H_CatalogId.Value == "0" )
                {
                    entTour.Id = 0;
                    updateStatus = DataRepository.TourProvider.Insert(entTour);
                }
                else
                {
                    entTour.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.TourProvider.Update(entTour);
                    RedirectTo("~/Admin/Tour/QLTour.aspx");
                }
                if (!updateStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TOUR_CAPNHAT_THATBAI, true);
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.TOUR_CAPNHAT_THANHCONG, true);
                    H_CatalogId.Value = entTour.Id.ToString();
                }
            }

        }

        catch (Exception ex)
        {
            Common.WriteLog("CNTour.aspx.cs", "cmdLuu_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TOUR_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        H_AnhDaiDien.Value = "no_image.jpg";
        txtTieuDeTin.Text = "";
        txtTomTat.Text = "";
        chkTrangThai.Checked = true;
        txtContent.Text = "";
        txtNgayDi.Text = "";
        txtNgayVe.Text = "";
        txtGioDi.Text = "";
        txtGioDen.Text = "";
        txtSoCho.Text = "";
        txtThongTin.Text = "";
        txtDieuKhoan.Text = "";
        txtGiaTour.Text = "";
        BindCatalog.BindSubTour(cboSubTour);
        BindCatalog.BindLoaiTour(cboLoaiTour);
        BindCatalog.BindNoiDi(cboDiemDi);
        BindCatalog.BindNoiDen(cboDiemDen);
    }
    protected void SelectedindexchangedcboLoaiTour(object sender, EventArgs e)
    {
        TList<SubTour> TList_Catalogs = DataRepository.SubTourProvider.Find("IdLoaiTour=" + cboLoaiTour.SelectedValue);
        cboSubTour.DataSource = TList_Catalogs;
        cboSubTour.DataTextField = "TenSubTour";
        cboSubTour.DataValueField = "Id";
        cboSubTour.DataBind();
        TList<NoiDen> TList_Catalogs1 = DataRepository.NoiDenProvider.Find("IdLoaiTour=" + cboLoaiTour.SelectedValue);
        if (TList_Catalogs1.Count > 0)
        {
            cboDiemDen.DataSource = TList_Catalogs1;
            cboDiemDen.DataTextField = "TenNoiDen";
            cboDiemDen.DataValueField = "Id";
            cboDiemDen.DataBind();
        }
        else
            BindCatalog.BindNoiDen(cboDiemDen);
    }

    private void RedirectTo(string url)
    {
        string redirectURL = Page.ResolveClientUrl(url);
        string script = String.Format("window.location = '{0}';", redirectURL);
        ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);
    }
}